package com.ht.utils.htmlBean;

import lombok.Data;

import java.util.ArrayList;
import java.util.LinkedList;
@Data
public class Table {

    private Line top = null;
    private Line bottom = null;
    private Line left = null;
    private Line right = null;
    private float margin_left = 0;
    private float margin_top = 0;
    private float width = 0;
    private ArrayList<Float> widths = new ArrayList<>();
    private LinkedList<TR> trs = new LinkedList<>();

    public void addWidths(Float width){
        this.width = this.width + width;
        this.widths.add(width);
    }

    public void addTr(TR tr){
        this.trs.add(tr);
    }

    String toHtml(){
        StringBuilder html = new StringBuilder("<table style=\"border-collapse:collapse;");
        if(this.top != null){
            html.append("border-top-style:").append(Line.converLineType(this.top.getType()))
                    .append(";border-top-color:").append(Line.converColor(this.top.getColor()))
                    .append(";border-top-width:").append(this.top.getSz() / 250 * 12 / 0.75).append("px;");
        }
        if(this.bottom != null){
            html.append("border-bottom-style:").append(Line.converLineType(this.bottom.getType()))
                    .append(";border-bottom-color:").append(Line.converColor(this.bottom.getColor()))
                    .append(";border-bottom-width:").append(this.bottom.getSz() / 250 * 12 / 0.75).append("px;");
        }
        if(this.left != null){
            html.append("border-left-style:").append(Line.converLineType(this.left.getType()))
                    .append(";border-left-color:").append(Line.converColor(this.left.getColor()))
                    .append(";border-left-width:").append(this.left.getSz() / 250 * 12 / 0.75).append("px;");
        }
        if(this.right != null){
            html.append("border-right-style:").append(Line.converLineType(this.right.getType()))
                    .append(";border-right-color:").append(Line.converColor(this.right.getColor()))
                    .append(";border-right-width:").append(this.right.getSz() / 250 * 12 / 0.75).append("px;");
        }
        if(this.width > 0){
            html.append("width:").append(this.width * 100).append("%;");
        }
        if(this.margin_left > 0){
            html.append("margin-left:").append(this.margin_left).append("px;");
        }
        if(this.margin_top > 0){
            html.append("margin-top:").append(this.margin_top).append("px;");
        }
        StringBuilder widths = new StringBuilder();
        if(!this.widths.isEmpty()){
            for(int i = 0; i < this.widths.size(); i++){
                Float one = (Float) this.widths.get(i);
                if(i != 0){
                    widths.append("-");
                }
                widths.append(one);
            }
        }
        html.append("\" border='1' data-class='table' table-widths='").append(widths.toString()).append("'>");
        if(trs != null && !trs.isEmpty()){
            for(TR tr : trs){
                html.append(tr.toHtml());
            }
        }
        html.append("</table>");
        return html.toString();
    }

}
